package com.example.demo.service;


import com.example.demo.dao.RolePrivilegeMapper;
import com.example.demo.entity.RolePrivilege;
import com.example.demo.entity.UserRole;
import com.example.demo.pojo.BSResult;
import com.example.demo.utils.BSResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
public class RolePrivilegeServiceImpl implements RolePrivilegeService {

    @Autowired
    private RolePrivilegeMapper rolePrivilegeMapper;


    @Override
    public int deleteByRoleId(Integer roleId) {
        Example example=new Example(UserRole.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("role_id",roleId);

        return rolePrivilegeMapper.deleteByExample(example);
    }


    @Override
    @Transactional
    public BSResult updatePrivRole(Integer[] privIds, Integer roleId) {


            Example example=new Example(RolePrivilege.class);

            Example.Criteria criteria=example.createCriteria();

            criteria.andEqualTo("role_id",roleId);

            rolePrivilegeMapper.deleteByExample(example);


            for(Integer privId:privIds){
                RolePrivilege rolePrivilege=new RolePrivilege();
                rolePrivilege.setRole_id(roleId);
                rolePrivilege.setPrivilege_id(privId);
                rolePrivilege.setCreated(new Date());
                rolePrivilege.setUpdated(new Date());
                rolePrivilegeMapper.insert(rolePrivilege);
            }

            return BSResultUtils.success(roleId);



    }
}
